import axios from "axios";
import { ElLoading } from 'element-plus'
import { ElMessage } from 'element-plus'
import store from "../store/index.js"
// 使用create创建axios实例
let LoadingObj = null
const Service = axios.create({
    timeout: 8000,// 超时
    baseURL: 'http://127.0.0.1:8888/api/private/v1/', // 接口网址
    headers: {// 请求头
        "Content-type": "application/json;charset=utf-8",
        "Authorization": store.state.uInfo.userInfo.token
    }
})
// 请求拦截-增加loading,对请求做统一处理
Service.interceptors.request.use(config => {
    LoadingObj = ElLoading.service({
        lock: true,
        text: 'Loading',
        background: 'rgba(0, 0, 0, 0.7)',
    })
    return config
})

// 响应拦截-对返回值做统一处理
Service.interceptors.response.use(response => {
    LoadingObj.close()//关闭Loading
    const data = response.data
    if (data.meta.status != 200 && data.meta.status != 201) {
        // 请求出错
        ElMessage.error(data.meta.msg || "服务器出错")
        return data
    }
    return data
}, error => {
    LoadingObj.close()//关闭Loading
    ElMessage({
        message: '服务器错误！',
        type: 'error',
        duration: 2000
    })
})

// post请求
export const post = config => {
    return Service({
        ...config,
        method: 'post',
        data: config.data
    })
}
// get请求
export const get = config => {
    return Service({
        ...config,
        method: 'get',
        params: config.data
    })
}

// put请求
export const put = config => {
    return Service({
        ...config,
        method: 'put',
        data: config.data
    })
}

// delete请求
export const del = config => {
    return Service({
        ...config,
        method: 'delete'
    })
}